package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.MessageBlack;
import cn.itsource.ymcc.domain.MessageSms;
import cn.itsource.ymcc.dto.DateAndCode;
import cn.itsource.ymcc.dto.MobileDTO;
import cn.itsource.ymcc.exception.GlobleException;
import cn.itsource.ymcc.properties.VerifyCodePro;
import cn.itsource.ymcc.service.IMessageBlackService;
import cn.itsource.ymcc.service.IMessageSmsService;
import cn.itsource.ymcc.service.IVerifyService;
import cn.itsource.ymcc.util.AssertUtil;
import cn.itsource.ymcc.util.VerifyCodeUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyServiceImpl implements IVerifyService {
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    private IMessageSmsService iMessageSmsService;
    @Autowired
    private VerifyCodePro verifyCodePro;
    @Autowired
    private IMessageBlackService iMessageBlackService;
    @Override
    public void sendSmsCode(MobileDTO dto) {
        String imageCode = dto.getImageCode();
        String mobile = dto.getMobile();
        //电话号码黑名单校验
        String tel = dto.getMobile();
        List<MessageBlack> messageBlack1 = (List<MessageBlack>)redisTemplate.opsForValue().get("messageBlack");
        if (messageBlack1==null){
            List<MessageBlack> messageBlacks = iMessageBlackService.selectList(null);
            redisTemplate.opsForValue().set("messageBlack",messageBlacks,12,TimeUnit.HOURS);
            messageBlacks.forEach(messageBlack -> {
                if (messageBlack.getTel().equals(tel)){
                    AssertUtil.isNull(messageBlack,"该号码已经是黑名单");
                }
            });
        }else {
            messageBlack1.forEach(messageBlack -> {
                if (messageBlack.getTel().equals(tel)){
                    AssertUtil.isNull(messageBlack,"该号码已经是黑名单");
                }
            });
        }

        String imageCodeKey = dto.getImageCodeKey();
        Object o = redisTemplate.opsForValue().get(imageCodeKey);
        if (o == null){
            throw new GlobleException("验证码过期");
        }
        if (!o.toString().equalsIgnoreCase(imageCode)){
            throw new GlobleException("验证码错误");
        }
        Date date = new Date();
        DateAndCode o1 = (DateAndCode)redisTemplate.opsForValue().get(mobile);
        String code = null;
        if (o1 != null){
            if ((date.getTime() - o1.getDate()) / 1000 > verifyCodePro.getTime()){
                code = o1.getCode();
            } else {
                System.out.println(o1.getCode());
                throw new GlobleException("请稍后");
            }
        } else {
            code = VerifyCodeUtils.generateVerifyCode(6);
        }
        //设置

        redisTemplate.opsForValue().set(mobile,new DateAndCode(code, date.getTime()),5,TimeUnit.MINUTES);
        //发短信
        //SmsUtil.sendSms(phone, "你的验证码为：" + code + "，请在3分钟之内使用!" );  //有次数
        String format = String.format(verifyCodePro.getContent(), code); // 拼接
        System.out.println(format);//可以这样测试
        MessageSms messageSms = new MessageSms();
        messageSms.setTitle("电话验证码");
        messageSms.setContent(format);
        //messageSms.setContent("你的验证码为：" + code + "，请在5分钟之内使用!");
        messageSms.setSendTime(date);
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        String ip = requestAttributes.getRequest().getRemoteAddr();
        messageSms.setIp(ip);
        iMessageSmsService.insert(messageSms);
        System.out.println(verifyCodePro.getTime());
    }

    @Override
    public String sendImage(String key) {
        //判断key是否为空
        if(StringUtils.isEmpty(key)){
            throw new GlobleException("key为空","5");
        }
        String s = VerifyCodeUtils.generateVerifyCode(4);
        redisTemplate.opsForValue().set(key,s,5, TimeUnit.MINUTES);
        //把验证码的值合并到图片，使用Base64编码。并返回base64编码的字符串
        return VerifyCodeUtils.verifyCode(110, 40, s);
    }
}
